﻿/************************************************************************

 This file is part of EscapeFromPrisonPlanet.

 EscapeFromPrisonPlanet is free software: you can redistribute it and/or modify
 it under the terms of the GNU General Public License as published by
 the Free Software Foundation, either version 3 of the License, or
 (at your option) any later version.

 EscapeFromPrisonPlanet is distributed in the hope that it will be useful,
 but WITHOUT ANY WARRANTY; without even the implied warranty of
 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 GNU General Public License for more details.

 You should have received a copy of the GNU General Public License
 along with Foobar.  If not, see <http://www.gnu.org/licenses/>.

************************************************************************/

// All code (c)2012 Scott Bevin. all rights reserved

using System;
using System.Collections.Generic;
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Content;
using Microsoft.Xna.Framework.Graphics;

namespace LudumDare25Project.Objects
{
    internal class AsteroidBelt
    {
        public class Asteroid
        {
            public Vector2 Position;
            public Texture2D Sprite;
            public float Scale;
            public float Rotation;
            public float Speed;
            public float Radius;
        }

        public List<Texture2D> asteroidSprites = new List<Texture2D>();
        private List<Asteroid> asteroids = new List<Asteroid>();
        private Random rnd = new Random();

        public float XSpeed = 120.0f;
        private float yPos;
        private float screenWidth;
        private float rangeMult;
        private float timeTillNext = 0.0f;

        public AsteroidBelt(ContentManager content, float yPos, float screenWidth, float rangeMult)
        {
            this.yPos = yPos;
            this.screenWidth = screenWidth;
            this.rangeMult = rangeMult;

            bool done = false;
            int counter = 1;

            while (!done)
            {
                try
                {
                    Texture2D tex = content.Load<Texture2D>("Img/Asteroids/A" + counter.ToString());
                    asteroidSprites.Add(tex);
                    counter++;
                }
                catch (Exception e)
                {
                    done = true;
                }
            }

            /* float pos = -2000.0f;
             for (int i = 0; i < 100; ++i)
             {
                 Update(1.0f, pos);
                 pos += 100.0f;
             }*/
        }

        public void Update(float dt, float camFocusPoint, float speedMod)
        {
            for (int i = 0; i < asteroids.Count; )
            {
                if (asteroids[i].Position.X > camFocusPoint - 1000.0f)
                {
                    asteroids[i].Position.X -= asteroids[i].Speed * speedMod * dt;
                    asteroids[i].Rotation -= 0.05f * dt;
                    ++i;
                }
                else
                {
                    asteroids.RemoveAt(i);
                }
            }

            if ((timeTillNext -= dt) <= 0.0f)
            {
                timeTillNext += 0.25f;

                Asteroid newA = new Asteroid();
                newA.Position = new Vector2(camFocusPoint + 1000.0f, yPos + RandomFloat(0.0f, 2000.0f * rangeMult));
                newA.Rotation = RandomFloat(0, MathHelper.TwoPi);
                newA.Scale = RandomFloat(0.75f, 1.25f);
                newA.Sprite = asteroidSprites[rnd.Next(asteroidSprites.Count)];
                newA.Speed = XSpeed * RandomFloat(0.75f, 1.5f);

                asteroids.Add(newA);

                newA = new Asteroid();
                newA.Position = new Vector2(camFocusPoint + 1000.0f, yPos);
                newA.Rotation = RandomFloat(0, MathHelper.TwoPi);
                newA.Scale = RandomFloat(0.75f, 1.25f);
                newA.Sprite = asteroidSprites[rnd.Next(asteroidSprites.Count)];
                newA.Speed = XSpeed * RandomFloat(0.75f, 1.5f);

                asteroids.Add(newA);
            }
        }

        public float RandomFloat(float min, float max)
        {
            return min + (max - min) * (float)rnd.NextDouble();
        }

        public void Render(SpriteBatch sprites)
        {
            foreach (Asteroid ast in asteroids)
            {
                sprites.Draw(
                    ast.Sprite,
                    ast.Position,
                    null,
                    Color.White,
                    ast.Rotation,
                    new Vector2(ast.Sprite.Width, ast.Sprite.Height) * 0.5f,
                    ast.Scale,
                    SpriteEffects.None,
                    0.0f);
            }
        }
    }
}